#!/usr/bin/env python

# Load the jinja library's namespace into the current module.
import jinja2
import os
from collections import OrderedDict

if not os.path.isdir("generated"):
    os.makedirs("generated")

templateLoader = jinja2.FileSystemLoader( searchpath="templates" )
templateEnv = jinja2.Environment( loader=templateLoader )

# This constant string specifies the template file we will use.
TEMPLATE_FILE_IMAGE_MENU_C = "image_menu_c.jinja"
template_image_menu_c = templateEnv.get_template( TEMPLATE_FILE_IMAGE_MENU_C )

TEMPLATE_FILE_LIST_MENU_C = "list_menu_c.jinja"
template_list_menu_c = templateEnv.get_template( TEMPLATE_FILE_LIST_MENU_C )

TEMPLATE_FILE_MENU_H = "menu_h.jinja"
template_menu_h = templateEnv.get_template( TEMPLATE_FILE_MENU_H )

TEMPLATE_FILE_MODE_H = "mode_h.jinja"
template_mode_h = templateEnv.get_template( TEMPLATE_FILE_MODE_H )

TEMPLATE_FILE_MODE_C = "mode_c.jinja"
template_mode_c = templateEnv.get_template( TEMPLATE_FILE_MODE_C )

# Specify any input variables to the template as a dictionary.
templateVarss = []
templateVarss += [{ "menu_name" : "modes",
                 "items" : ['video_mode','photo_mode','burst_mode','timelapse_mode'] }]#,'settings' ] }]

# templateVarss += [{ "menu_name" : "settings",
#                  "items" : ['video_resolution_modes','photo_resolution_modes','continuous_photo','photo_burst_option','timelapse' ] }] # after ,'capture_settings' ] }]

# templateVarss += [{ "menu_name" : "capture_settings",
#                  "items" : ['upside_down','spotmeter','simultaneous','looping_video','protune','white_balance'] }]

for templateVars in templateVarss:
	# Finally, process the template to produce our final text.
	outputText = template_image_menu_c.render( templateVars )

	fo = open("generated/menu_"+templateVars['menu_name'] +".c","w+")
	fo.write(outputText)
	fo.close()

	outputText = template_menu_h.render( templateVars )

	fo = open("generated/menu_"+templateVars['menu_name'] +".h","w+")
	fo.write(outputText)
	fo.close()


# Specify any input variables to the template as a dictionary.
templateVarss = []
templateVarss += [{ "mode_name" : "video_mode", "mode_value" : 0}]
templateVarss += [{ "mode_name" : "photo_mode", "mode_value" : 1}]
templateVarss += [{ "mode_name" : "burst_mode", "mode_value" : 2}]
templateVarss += [{ "mode_name" : "timelapse_mode", "mode_value" : 3}]


for templateVars in templateVarss:
    # Finally, process the template to produce our final text.
    outputText = template_mode_c.render( templateVars )

    fo = open("generated/menu_"+templateVars['mode_name'] +".c","w+")
    fo.write(outputText)
    fo.close()

    outputText = template_mode_h.render( templateVars )

    fo = open("generated/menu_"+templateVars['mode_name'] +".h","w+")
    fo.write(outputText)
    fo.close()



# Specify any input variables to the template as a dictionary.
templateVarss = []
templateVarss += [{ 
	"menu_name" : "photo_resolution_modes",
    "items" : OrderedDict([('12 MP Wide',5),('7 MP Wide',4),('7 MP Med',6),('5 MP Med',3)]),
    "command_name" : 'PR',
    "index_in_data" : 8
    }]

templateVarss += [{ 
	"menu_name" : "video_resolution_modes",
    "items" : OrderedDict([('4Kcin',8),('2.7Kcin',7),('4K',6),('2.7K',5),( '1440',4),( '1080',3),( '960',2),( '720',1),( 'WVGA',0)]) ,
    "command_name" : 'VV',
    "index_in_data" : 50
    }]

templateVarss += [{ 
	"menu_name" : "continuous_photo",
    "items" : OrderedDict([('Single',0),('3sps',0X03),('5sps',0X05),('10sps',0X0A)]) ,
    "command_name" : 'CS',
    "index_in_data" : 33
    }]

templateVarss += [{ 
	"menu_name" : "photo_burst_option",
    "items" : OrderedDict([('3/1s',0),('5/1s',0X01),('10/1s',0X02),('10/2s',0X03),('30/1s',0X04),('30/2s',0X05),('30/3s',0X06)]) ,
    "command_name" : 'BU',
    "index_in_data" : 32
    }]

templateVarss += [{ 
	"menu_name" : "timelapse",
    "items" : OrderedDict([('0.5s',0),('1s',0X01),('2s',0X02),('5s',0X05),('10s',0X0a),('30s',0X1e),('60s',0X3c)]) ,
    "command_name" : 'TI',
    "index_in_data" : 5
    }]

# templateVarss += [{ 
# 	"menu_name" : "upside_down",
#     "items" : OrderedDict([('Up',0),('Down',0X01)]) ,
#     "command_name" : 'UP',
#     "index_in_data" : 18,
#     "bit_index" : 2
#     }]

# templateVarss += [{ 
# 	"menu_name" : "spotmeter",
#     "items" : OrderedDict([('Off',0),('On',0X01)]) ,
#     "command_name" : 'EX',
#     "index_in_data" : 4
#     }]

# templateVarss += [{ 
# 	"menu_name" : "simultaneous",
#     "items" : OrderedDict([('Off',0),('5s',0X01),('10s',0X02),('30s',0X03),('60s',0X04)]) ,
#     "command_name" : 'PN',
#     "index_in_data" : 36
#     }]

# templateVarss += [{ 
# 	"menu_name" : "looping_video",
#     "items" : OrderedDict([('Off',0),('5 min',0X01),('20 min',0X02),('60 min',0X03),('120 min',0X04),('MAX',0X05)]) ,
#     "command_name" : 'LO',
#     "index_in_data" : 0 #!!!!!!!!!!!!!!!!!!!!!!!!! tbd
#     }]

# templateVarss += [{ 
# 	"menu_name" : "protune",
#     "items" : OrderedDict([('Off',0),('On',0X01)]) ,
#     "command_name" : 'PT',
#     "index_in_data" : 30,
#     "bit_index" : 1
#     }]

# templateVarss += [{ 
# 	"menu_name" : "white_balance",
#     "items" : OrderedDict([('Auto',0),('3000K',0X01),('5500K',0X02),('6500K',0X03),('CAM RAW',0X04)]) ,
#     "command_name" : 'WB',
#     "index_in_data" : 34
#     }]

templateVarss += [{ 
    "menu_name" : "fps",
    "items" : OrderedDict([('12',0),('15',0X01),('24',0X02),('25',0X03),('30',0X04),('48',0X05),('50',0X06),('60',0X07),('100',0X08),('120',0X09),('240',0X0a)]) ,
    "command_name" : 'FS',
    "index_in_data" : 51
    }]

for templateVars in templateVarss:
	# Finally, process the template to produce our final text.
	outputText = template_list_menu_c.render( templateVars )

	fo = open("generated/menu_"+templateVars['menu_name'] +".c","w+")
	fo.write(outputText)
	fo.close()

	outputText = template_menu_h.render( templateVars )

	fo = open("generated/menu_"+templateVars['menu_name'] +".h","w+")
	fo.write(outputText)
	fo.close()

